Scripting

nShell(tm) scripts are simple text files.  They may be created and edited in any text editor, including the TeachText and SimpleText programs provided with your Macintosh.

To create a script, type the series of commands you wish to execute into a file and save it in your current working directory.  (The shell searches your "command search path" for scripts just as it does commands.)

You can put comments in scripts using the "#" character.

Simple Scripts

As an example, consider the "echo" command.  Echo repeats a string to the shell window, as in:

% echo "this is me"
this is me

If I were to take these lines:

# This is a comment
echo "I am a script"
echo "and I am fine."

and save them in a text file called "yak", I could type yak and get the following:

% yak
I am a script
and I am fine.

Scripts can call other scripts just as they can call commands.  If I were to take the lines:

# This time we do it twice
yak
echo "tell me again?"
yak

and save them in a file called "two", I could type two and get the following:

% two
I am a script
and I am fine.
tell me again?
I am a script
and I am fine.

Script Parameters

You can pass parameters to scripts just as you can with commands.  When a script is executed its parameters are stored in shell variables.  The variables used are $#, $0, $1, $2...

$# contains the number of passed parameters
$0 contains the name of the script
$1 contains the first parameter
$2 contains the second parameter
...

We can demonstrate this with the simple script called "kitty":

echo $#
echo $0
echo $1
echo $2

Running kitty, we get:

% kitty is fine
3
kitty
is
fine

Variables in Scripts

In addition to shell parameters, the "set" "unset" and "env" commands may be used to create and modify variables within a script.

It is important to note that while a script may modify any variable, all changes are temporary and are discarded when the script terminates.